package com.lsl.gencode.web;

import com.alibaba.fastjson.JSONObject;
import com.lsl.easycode.common.GenType;
import com.lsl.easycode.core.DbQuery;
import com.lsl.easycode.core.GenerateCode;
import com.lsl.easycode.utils.StringUtil;
import com.lsl.easycode.vo.GenValue;
import com.lsl.easycode.vo.Result;
import com.lsl.gencode.util.ViewRender;
import org.beetl.core.Configuration;
import org.beetl.ext.servlet.ServletGroupTemplate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.lsl.easycode.core.DbQuery.queryALlTable;
import static javafx.scene.input.KeyCode.J;

/**
 * Created by liusl on 2016/12/26.
 */
public class GenController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        List<String> tableList= DbQuery.queryALlTable();
        if (tableList==null){
            tableList=new ArrayList<>();
            request.setAttribute("tipInfos","<label class='label label-danger'>连接数据库失败</label>");
        }
        request.setAttribute("tableList",tableList);
        ViewRender.rend(request, response,"index.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setHeader("content-type", "text/html;charset=UTF-8");
        String modelname = req.getParameter("modelname");
        String tablename = req.getParameter("tablename");
        String sqlmappath = req.getParameter("sqlmappath");
        String mapperpath = req.getParameter("mapperpath");
        String entitypath = req.getParameter("entitypath");
        if (StringUtil.blank(tablename)){
            Result result=new Result();
            result.setSuccess(false);
            result.setTips("<label class='label label-danger'>表名不能为空</label>");
            resp.getWriter().write(JSONObject.toJSONString(result));
            return ;
        }
        GenerateCode generateCode = GenerateCode.genCode;
        try {
            GenValue genValue = new GenValue();
            genValue.setEntity(entitypath);
            genValue.setMapper(mapperpath);
            genValue.setXml(sqlmappath);
            genValue.setTableName(tablename);
            genValue.setName(modelname);
            GenType genType = new GenType();
            Result result = generateCode.genCode(genValue, genType);
            String restr=JSONObject.toJSONString(result);
            resp.getWriter().write(restr);
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write("生成失败");
        }finally {
            resp.getWriter().flush();
            resp.getWriter().close();
        }
    }
}
